using System;
using System.Collections.Generic;
using GameFramework.Event;
using UnityEditor;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce.Hotfix
{
    public class ProcedureHotfixMenu : ProcedureHotfixBase
    {
        private bool m_StartGame = false;
        
        private GameMode m_CurGameMode;
        
        public void StartGame()
        {
            m_StartGame = true;
        }

        
        protected internal override void OnEnter(IFsm procedureOwner)
        {
            base.OnEnter(procedureOwner);
            Log.Info("进入menu流程");
            if (!GameEntry.Http.IsLoaded)
            {
                GameEntry.Http.Load();
            }
            
            GameEntry.UI.OpenUIForm((int) HotfixUIFormId.LoginPage);
            
            HotfixGameEntry.Event.Subscribe(GameStartEventArgs.EventId,OnStartGame);
        }

        private void OnStartGame(object sender, HotfixGameEventArgs e)
        {
            GameStartEventArgs result = (GameStartEventArgs) e;
            m_CurGameMode = result.GameMode;
            m_StartGame = true;
        }
        
        protected internal override void OnUpdate(IFsm procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (m_StartGame)
            {
                procedureOwner.SetData<VarInt>("NextSceneId", GameEntry.Config.GetInt("Scene.Main"));
                procedureOwner.SetData<VarInt>("GameMode", (int)m_CurGameMode);
                ChangeState<ProcedureHotfixMain>(procedureOwner);
            }
        }

        protected internal override void OnLeave(IFsm procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            HotfixGameEntry.Event.Unsubscribe(GameStartEventArgs.EventId,OnStartGame);
        }
    }
}